package com.test.demo.service;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

@Service
public class CustomClientDetailsService implements ClientDetailsService {
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 重写 loadClientByClientId 方法
     * 在请求token的时候，会携带clientId，此时根据clientId进行查找
     * @param clientId The client id.
     * @return
     * @throws ClientRegistrationException
     */
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        // 根据clientId，从redis或者mysql中获取client信息
        BaseClientDetails client = new BaseClientDetails(
                "client", "", "all", "authorization_code,password,client_credentials", "", ""
        );
        client.setClientSecret(passwordEncoder.encode("111111"));
        client.setRegisteredRedirectUri(CollUtil.newHashSet("https://www.baidu.com"));
        client.setAutoApproveScopes(CollUtil.toList("true"));

        return client;
    }
}
